﻿using System;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using System.IO; //System.IO is not used by default
//using PCComm;

namespace PCComm
{
    class ECMCommunication : CommunicationManager
    {
        #region ECM Enums
        /// <summary>
        /// enumeration to hold our Command types the ECM can respond to
        /// </summary>
        public enum ECMCommandType : byte
        { 
            ECMGet = 0x52, 
            ECMSet = 0x57, 
            ECMVersion = 0x56, 
            ECMRunTimeData = 0x43
        }

        /// <summary>
        /// enumeration to hold our special function data that can be requested from the ECM via virtual page 0x20
        /// </summary>
        public enum ECMVirtualPageFunction : byte 
        { 
            ClearCodes = 0x01, 
            FrontCoil = 0x02, 
            RearCoil = 0x03, 
            Tachometer = 0x04, 
            FuelPump = 0x05,
            FrontInj = 0x06, 
            RearInj = 0x07, 
            TPSReset = 0x08, 
            Fan = 0x09, 
            ExhaustValve = 0x0A
        }

        #endregion

        private object parent;

        #region ECM Constructors
        /// <summary>
        /// Constructor to set the properties of our ECM Class
        /// </summary>
        public ECMCommunication(object parent)
        {
            comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

        }
        #endregion

        #region comPort_DataReceived extension
        /// <summary>
        /// method that will be called when theres data waiting in the buffer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (CurrentTransmissionType == TransmissionType.ECM)
            {
                //retrieve number of bytes in the buffer
                int bytes = comPort.BytesToRead;
                //create a byte array to hold the awaiting data
                byte[] comBuffer = new byte[bytes];
                //read the data and store it
                comPort.Read(comBuffer, 0, bytes);
                //display the data to the user
                DisplayData(MessageType.Incoming, " Dash Request: " + ByteToHex(comBuffer) + "\n");
                ECMAnalyseDashRequest(comBuffer);
            }
            else 
            {
                base.comPort_DataReceived(sender, e);    
            }
        }
        #endregion

        #region Analyse Dash Request
        /// <summary>
        /// method to find out what has been requested from the ECM
        /// </summary>
        /// <param name="receivedByte">byte array that contains Command Code in the 7th Byte</param>
        /// <returns>a byte in hex containing the Command code</returns>
        protected byte ECMAnalyseDashRequest(byte[] receivedByte)
        {
            switch (receivedByte[6])
            {
                case (byte)ECMCommandType.ECMGet:
                    DisplayData(MessageType.Error, "ECMGet is not supported yet" + "\n");
                    break;
                case (byte)ECMCommandType.ECMSet:
                    DisplayData(MessageType.Error, "ECMSet is not supported yet" + "\n");
                    break;
                case (byte)ECMCommandType.ECMVersion:
                    DisplayData(MessageType.Error, "ECMVersion is not supported yet" + "\n");
                    break;
                case (byte)ECMCommandType.ECMRunTimeData:
                    DisplayData(MessageType.Error, "ECMRunTimeData is not supported yet" + "\n");
                    break;
                default:
                    DisplayData(MessageType.Error, "Unknown message" + "\n");
                    break;
            }
            return receivedByte[6] ;
        }
        #endregion

        #region Build Runtime Response
        /// <summary>
        /// method to find out what has been requested from the ECM
        /// </summary>
        /// <param name="receivedByte">byte array that contains Command Code in the 7th Byte</param>
        /// <returns>a byte in hex containing the Command code</returns>
        protected void ECMBuildRunTimeResponse()
        {
            string s = this.ColumntoString("dgRunTimeData", "Hex_Data");
        }
        #endregion

    }
}
